<?php
/**
 * Created by PhpStorm.
 * User: lee
 * Date: 2018/1/9
 * Time: 下午3:39
 */

namespace Biz\model\activity;


class FullShippingGoods extends \Doraemon\pockets\datebase\BaseDbModel
{
    const _CONFIG_FILE_ = 'db_shop';

    const _CONFIG_SELECT_ = 'db_activity';

    const _DATABASE_ = 'shop_activity';

    const _TABLE_NAME = 'full_shipping_goods';


    //状态
    const STATUS_ACTIVE = 1; //正常
    const STATUS_INVALID = 0; // 失效
    const STATUS_DELETE = 9; //删除状态

    public static function addData($shopId, $shippingId, $startTime, $endTime, array $goodsIds)
    {
        $goodsIds = array_values($goodsIds);
        $data = array_map(function ($goodsId) use ($shopId, $shippingId, $startTime, $endTime) {
            $data = [];
            $data['shop_id'] = $shopId;
            $data['shipping_id'] = $shippingId;
            $data['goods_id'] = $goodsId;
            $data['start_time'] = $startTime;
            $data['end_time'] = $endTime;
            $data['status'] = self::STATUS_ACTIVE;
            return $data;
        }, $goodsIds);
        $ret = self::insert(array_filter($data));
        if ($ret->errorCode() !== '00000') {
            return false;
        }
        return true;
    }

    public static function updateData($data, $where)
    {
        $data['edittime'] = date('Y-m-d H:i:s');
        $ret = self::update($data, $where);
        if ($ret->errorCode() !== '00000') {
            return false;
        }
        return true;
    }

    public static function getList($where, $filter = '*')
    {
        return self::select($filter, $where);
    }

    public static function getShippingByGoodsId($goodsId, $filter = '*')
    {
        $now = date("Y-m-d H:i:s");
        $where = [
            'goods_id' => $goodsId,
            'end_time[>]' => $now,
            'status' => self::STATUS_ACTIVE
        ];

        $list = self::select($filter, $where);
        return $list;
    }


}